/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Services;

import java.io.Serializable;
import javax.persistence.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import Entity.Pet;
import java.util.List;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author microraptor
 */
public class PetService implements Serializable{
    
    private EntityManagerFactory emf = null;
    
    public PetService (EntityManagerFactory emf){
        this.emf = emf;
    }
    
    public EntityManager getEntityManager(){
        return emf.createEntityManager();
    }
    
    public Pet findPet(Long id){
        EntityManager em = null;
        Pet pet = null;
        try{
            em = getEntityManager();
            pet = em.find(Pet.class, id);
            System.out.println(pet.getOwner().getId());
        }finally{
            if(em != null){
                em.close();
            }
        }
        return pet;
    }
    
  
    
    public void crear(Pet pet){
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(pet);
            em.getTransaction().commit();
        }finally{
            if(em != null){
                em.close();
            }
        }
    }
    
    public void edit(Pet pet) throws Exception{
        EntityManager em = null;
        try{
            em = getEntityManager();
            em.getTransaction().begin();
            pet = em.merge(pet);
            em.getTransaction().commit();
        }catch (Exception ex){
            String msg = ex.getLocalizedMessage();
            if (msg == null ||  msg.length() == 0){
                Long id = pet.getId();
                if (findPet(id) == null){
                    throw new Exception("The pet with id "+id+" no longer exists.");
                }
            }
            throw ex;
        }finally{
            if (em != null){
                em.close();
            }
        }
    }
    
    public void destroy (Long id) throws Exception{
        EntityManager em = null;
        try{
            em=getEntityManager();
            em.getTransaction().begin();
            Pet pet;
            try{
                pet = em.getReference(Pet.class, id);
                pet.getId();
            }catch(EntityNotFoundException ex){
                throw new Exception ("The pet with id "+id+" no longer exists.", ex);
            }
            em.remove(pet);
            em.getTransaction().commit();
        }finally{
            if(em != null){
                em.close();
            }
        }
    }
    
    public List<Pet> findPetEntities(){
        return findPetEntities(true, -1,-1);
    }
    
    public List<Pet> findPetEntities(int maxResults, int firstResult){
        return findPetEntities(false, maxResults, firstResult);
    }
    
    private List<Pet> findPetEntities(boolean all, int maxResults, int firstResult)    {
        EntityManager em = getEntityManager();
        try{
            CriteriaQuery cq =  em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Pet.class));
            Query q = em.createQuery(cq);
            if(!all){
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        }finally{
            em.close();
        }
    }
}
